package com.jingxian.print.controller;

import com.jingxian.print.service.ImageService;
import com.jingxian.print.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.Base64;


/**
 * 图片pdf扩充Controller
 *
 * @author xuwei
 */
@Slf4j
@RestController
@RequestMapping("/imagePdf")
public class ImageExpandController  {

    @Autowired
    private ImageService imageService;


    @GetMapping(value = "/expand")
    public AjaxResult getInfo(@RequestParam String path) throws UnsupportedEncodingException {
        File parent = new File(path);
        if(!parent.exists()){
            return AjaxResult.error("文件夹不存在");
        }
        int pdf = 0 ;
        int img = 0;
        File[] files = parent.listFiles();
        for (File file : files) {
            if(file.getName().contains("pdf")){
                pdf++;
            }else{
                img++;
            }
        }
        if(pdf == files.length){
            imageService.doExpandPDF(path);
        }else if(img == files.length){
            imageService.doExpandIamge(path);
        }
        return AjaxResult.success();
    }

}
